package com.west_traveler.recyclerview_adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.west_traveler.entity.entity_ranking_page.RankingData;
import com.west_traveler.ximalayafm.R;

import java.util.List;

/**发现榜单界面中嵌套的RecyclerView适配器
 * created by Wang_JunJie at 2016/12/5
 */
public class RankingFrgRecyclerSecondAdapter extends RecyclerView.Adapter {


    private Context context;
    private RankingData rankingData ;

    private final int TYPE_EMPTY=1;
    private final int TYPE_NORMAL=2;

    public RankingFrgRecyclerSecondAdapter(Context context, RankingData rankingData) {
        this.context = context;
        this.rankingData = rankingData;

    }

    @Override
    public int getItemViewType(int position) {
        if (position%2!=0) {
            return TYPE_EMPTY;
        }else{
            return TYPE_NORMAL;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=null;
        switch (viewType) {
            case TYPE_EMPTY:
                 view = LayoutInflater.from(context).inflate(R.layout.between_items_empty, parent, false);

                EmptyViewHolder emptyViewHolder = new EmptyViewHolder(view);
                return emptyViewHolder;
            case  TYPE_NORMAL:
                 view = LayoutInflater.from(context).inflate(R.layout.fragment_ranking_item_peritem, parent, false);
                return new SecondViewHolder(view);
        }

        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SecondViewHolder) {
            int index=position/2;
            if (holder instanceof SecondViewHolder) {
                Picasso.with(context).load(rankingData.getList().get(index).getCoverPath()).into( ((SecondViewHolder) holder).ivPic);
                ((SecondViewHolder) holder).tvToatalTitle.setText(rankingData.getList().get(index).getTitle());
                ((SecondViewHolder) holder).tvTitleOne.setText("1 "+rankingData.getList().get(index).getFirstKResults().get(0).getTitle());
                ((SecondViewHolder) holder).tvTitleTwo.setText("2 "+rankingData.getList().get(index).getFirstKResults().get(1).getTitle());

            }
        }


    }

    @Override
    public int getItemCount() {
        return rankingData.getCount()*2-1;
    }

    public class SecondViewHolder extends RecyclerView.ViewHolder{

        private final ImageView ivPic;
        private final TextView tvToatalTitle;
        private final TextView tvTitleOne;
        private final TextView tvTitleTwo;

        public SecondViewHolder(View itemView) {
            super(itemView);
            ivPic = ((ImageView) itemView.findViewById(R.id.iv_frg_ranking_item_peritem));
            tvToatalTitle = ((TextView) itemView.findViewById(R.id.tv_frg_ranking_item_peritem_title));
            tvTitleOne = ((TextView) itemView.findViewById(R.id.tv_frg_ranking_item_peritem_pertitle_one));
            tvTitleTwo = ((TextView) itemView.findViewById(R.id.tv_frg_ranking_item_peritem_pertitle_two));
        }
    }


    public class EmptyViewHolder extends RecyclerView.ViewHolder{

        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }
}
